package in.muton.edushare.grading.panels.prevgrades.old;

import in.muton.edushare.common.UserFilter;
import in.muton.edushare.components.calendar.api.UniqueElementList;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.batch.BatchGradeEdit;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Users;

public class StudentSelection extends EdusharePanel {
	private IModel<Batches> batchModel;
	private String searchString = "";
	UniqueElementList<Users> selectedUsers = new UniqueElementList<Users>(null);

	public StudentSelection(String id, IModel<Batches> model,
			EduShareBasePage page) {
		super(id, model, page);
		this.batchModel = model;
		final Form<Void> form1;
		Form<Void> userSearch;
		add(form1 = new Form<Void>("form1") {
			@Override
			public void onSubmit() {
				if (selectedUsers.getObject().size() != 0) {
					setResponsePage(new BatchGradeEdit(batchModel,
							selectedUsers, 0));
				}
			}

			private static final long serialVersionUID = -3198265436759099300L;
		});
		form1.add(userSearch = new Form<Void>("userSearch"));
		userSearch.add(new TextField<String>("user", new PropertyModel<String>(
				this, "searchString")));

		final WebMarkupContainer wmc = new WebMarkupContainer("wmcForLV");
		form1.add(wmc);
		wmc.setOutputMarkupId(true);
		wmc.add(new ListView<IModel<Users>>("programLV", new FilterList()) {
			@Override
			protected void populateItem(final ListItem<IModel<Users>> arg0) {
				arg0.add(new EduShareURLPanel("name", arg0.getModel()
						.getObject()));
				arg0.add(new IndicatingAjaxLink<Void>("delete") {
					@Override
					public void onClick(AjaxRequestTarget target) {
						BatchDAO bDAO = new BatchDAO(getORMInterface());
						Users u = arg0.getModelObject().getObject();
						bDAO.removeStudent(batchModel.getObject(), u);
						target.addComponent(wmc);
					}

					private static final long serialVersionUID = -4852399092645350735L;
				});
				arg0.add(new CheckBox("selection", new Model<Boolean>() {
					private static final long serialVersionUID = 574248581740999888L;

					@Override
					public void setObject(Boolean bool) {
						selectedUsers.add(arg0.getModelObject());
					}

					@Override
					public Boolean getObject() {
						return selectedUsers.contains(arg0.getModelObject());
					}
				}));
			}

			private static final long serialVersionUID = -2862893929867464874L;
		});
	}

	private class FilterList extends InnerList<Users> {
		public List<IModel<Users>> getObject() {
			BatchDAO bDAO = new BatchDAO(getORMInterface());
			CayenneList<Users> cL1 = new CayenneList<Users>(bDAO
					.getUsers(batchModel.getObject()));
			return UserFilter.filterByString(cL1, searchString).getObject();
		}

		private static final long serialVersionUID = 2146472396669269447L;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	private static final long serialVersionUID = -8278000345437348911L;
}
